<?php
	/**
	 * Templater
	 * @author David Ivins
	 */
	class Templater extends Zend_View_Abstract
	{
		protected $_path;
		protected $_engine;
		
		/**
		 * __construct
		 */
		public function __construct()
		{
			$config = Zend_Registry::get('config');
			$subdomain = Zend_Registry::get('subdomain');
			
			require_once($config->path->lib->smarty->file);
			
			$this->_engine = new Smarty();	
			$this->_engine->force_compile = $config->smarty->compile->force;

			# determine which templates to use based on the subdomain
			if ($subdomain == $config->subdomain->mobile)
				$this->_engine->template_dir = $config->path->templates->mobile->dir;
			else if ($subdomain == $config->subdomain->text)
				$this->_engine->template_dir = $config->path->templates->text->dir;
			else
				$this->_engine->template_dir = $config->path->templates->default->dir;

			$this->_engine->compile_dir = sprintf('%s/templates_c', 
				$config->path->tmp->dir);
			
			$this->_engine->plugins_dir = array($config->path->templates->plugins->dir, 'plugins');
		}
		
		/** 
		 * getEngine
		 */
		public function getEngine()
		{
			return $this->_engine;
		}
		
		/**
		 * __set
		 */
		public function __set($key, $val)
		{
			$this->_engine->assign($key, $val);
		}
		
		/**
		 * __get
		 */
		public function __get($key)
		{
			return $this->_engine->get_template_vars($key);
		}
		
		/**
		 * __isset
		 */
		public function __isset($key)
		{
			return $this->_engine->get_template_vars($key) !== null;
		}
		
		/**
		 * __unset
		 */
		public function __unset($key)
		{
			$this->_engine->clear_assign($key);
		}
		
		/**
		 * __assign
		 */
		public function assign($spec, $value = null)
		{
			if (is_array($spec))
			{
				$this->_engine->assign($spec);
				return;
			}
			
			$this->_engine->assign($spec, $value);
		}
		
		/**
		 * __clearVars
		 */
		public function clearVars()
		{
			$this->_engine->clear_all_assign();
		}
		
		/**
		 * render
		 */
		public function render($name)
		{	
			return $this->_engine->fetch(strtolower($name));
		}	
		
		/**
		 * run
		 */
		public function _run() { }
	}
